/*
 * @Author: fisher
 * @Date: 2024-02-21 23:49:05
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-02-29 11:46:35
 * @FilePath: \allback\routes\upload.js
 * @Description:
 */
// routes/uploadRoutes.js
const Router = require('koa-router')
const router = new Router()
const dayjs = require('dayjs')
const fs = require('fs')
const path = require('path')
const { koaBody } = require('koa-body')
const uploadController = require('../controllers/uploadController')

router.post(
  '/file',
  koaBody({
    multipart: true,
    encoding: 'gzip', // 编码格式
    formidable: {
      uploadDir: (() => {
        // Use dayjs to format the current date as a folder name (e.g., "20230401" for April 1, 2023).
        const dateString = dayjs().format('YYYYMMDD')
        const uploadPath = path.join(__dirname, '../uploads', dateString)
        // Check if the directory exists, and create it if it does not.
        if (!fs.existsSync(uploadPath)) {
          fs.mkdirSync(uploadPath, { recursive: true })
        }
        return uploadPath
      })(),
      keepExtensions: true,
      // Accepts additional Formidable options if necessary.

      // onFileBegin: (name, file) => {
      //   // 文件上传前的设置可以在这里写
      //   console.log('file upload name=', name)
      //   console.log('file upload file=', file)
      // },
      onError: error => {
        throw new HttpError(error.message, 400)
      }
    }
    // Additional koa-body options can be added here if needed.
  }),
  uploadController.uploadFile
)

router.post(
  '/bigFile',
  koaBody({
    multipart: true,
    encoding: 'gzip', // 编码格式
    formidable: {
      uploadDir: (() => {
        // Use dayjs to format the current date as a folder name (e.g., "20230401" for April 1, 2023).
        // const dateString = dayjs().format('YYYYMMDD')
        const uploadPath = path.join(__dirname, '../bigfileUploads')
        // Check if the directory exists, and create it if it does not.
        if (!fs.existsSync(uploadPath)) {
          fs.mkdirSync(uploadPath, { recursive: true })
        }
        return uploadPath
      })(),
      // path.join(__dirname, '../bigfileUploads'),
      maxFileSize: 2000 * 1024 * 1024,
      keepExtensions: true,
      // Accepts additional Formidable options if necessary.

      // onFileBegin: (name, file) => {
      //   // 文件上传前的设置可以在这里写
      //   // console.log('file upload name=', name)
      //   // console.log('file upload file=', file)
      // },
      onError: error => {
        throw new HttpError(error.message, 400)
      }
    }
    // Additional koa-body options can be added here if needed.
  }),
  uploadController.uploadBigFile
)
// router.post('/combine', uploadController.combine)
// router.get('/combineSSE', uploadController.combineSSE)
router.get('/checkExistence', uploadController.checkExistence)
router.post('/merge', uploadController.merge)
router.get('/getMergeProgress/:taskId', uploadController.getMergeProgress)
router.get('/getUefiles', uploadController.getUefiles)
router.post('/deleteUefile', uploadController.deleteUefile)
module.exports = router
